﻿/*
=======================================================
    BillDetail【单据明细】
=======================================================
*/

namespace Services
{
    public interface IBillDetailMapping : IMapping<BillDetail, BillDetailModel, BillDetailFilterModel>
    { }

    public class BillDetailMapping : AutoBillDetailMapping, IBillDetailMapping
    {
        private readonly ISqlSugarClient _DB;

        public BillDetailMapping(ICurrentUser currentUser, ISqlSugarClient dB)
            : base(currentUser)
        {
            _DB = dB;
        }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override BillDetailModel Entity2Model(BillDetail entity)
        {
            if (entity == null)
            {
                return null;
            }

            BillDetailModel model = base.Entity2Model(entity);


            /***************************************** 下面处理自定义转换 *****************************************/
            model.Department_Name = entity.Department_Name;


            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override BillDetail Model2Entity(BillDetailModel model)
        {
            BillDetail entity = base.Model2Entity(model);


            /***************************************** 下面处理自定义转换 *****************************************/



            return entity;
        }
        #endregion

        #region 读数据(集合)
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entities">Entity对象</param>
        /// <returns></returns>
        public override List<BillDetailModel> Entity2Model(IEnumerable<BillDetail> entities)
        {
            return entities.Select(x => Entity2Model(x)).ToList();
        }
        #endregion

        #region 写数据(集合)
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="models">ViewModel对象</param>
        /// <returns></returns>
        public override List<BillDetail> Model2Entity(IEnumerable<BillDetailModel> models)
        {
            return models.Select(x => Model2Entity(x)).ToList();
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<BillDetail> FilterModel2Where(BillDetailFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //处理数据权限
            if (_currentUser.CurrentUserInfo.LoginUserType == LoginUserType.集团)
            {
                var billIDs = _DB.Queryable<Bill>().Where(x => _currentUser.CurrentUserInfo.LoginUserWarehouseIDsWithDataDepartment.Contains(x.WarehouseID))
                    .Select(x => x.BillID).Distinct().ToList();

                where = where.And(x => billIDs.Contains(x.BillID));
            }

            //SearchInput
            if (!string.IsNullOrEmpty(filterModel._SearchInput))
            { }

            //规格型号
            if (!string.IsNullOrEmpty(filterModel._GuiGe))
            {
                where = where.And(x => x.GuiGe.Contains(filterModel._GuiGe));
                //where = where.And(x => x.GuiGe == filterModel._GuiGe);
            }

            ////直径
            //if (filterModel._Diameter.HasValue)
            //{
            //    where = where.And(x => x.Diameter == filterModel._Diameter.Value);
            //}

            ////绳子条数
            //if (filterModel._Cnt.HasValue)
            //{
            //    where = where.And(x => x.Cnt == filterModel._Cnt.Value);
            //}

            ////公称捻距
            //if (filterModel._NianJu.HasValue)
            //{
            //    where = where.And(x => x.NianJu == filterModel._NianJu.Value);
            //}

            ////长度
            //if (filterModel._LengthNum.HasValue)
            //{
            //    where = where.And(x => x.LengthNum == filterModel._LengthNum.Value);
            //}

            ////备绳维保周期
            //if (filterModel._KeepDay.HasValue)
            //{
            //    where = where.And(x => x.KeepDay == filterModel._KeepDay.Value);
            //}

            //单据状态
            if (filterModel._BillStatuss.IsNotEmpty() && filterModel._BillStatuss.Count > 0)
            {
                var billIDs = _DB.Queryable<Bill>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && filterModel._BillStatuss.Contains(x.Status))
                    .Select(x => x.BillID).Distinct().ToList();

                where = where.And(x => billIDs.Contains(x.BillID));
            }

            //单据操作类型
            if (filterModel._BillOperTypes.IsNotEmpty() && filterModel._BillOperTypes.Count > 0)
            {
                var billIDs = _DB.Queryable<Bill>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && filterModel._BillOperTypes.Contains(x.OperTypeID))
                  .Select(x => x.BillID).Distinct().ToList();

                where = where.And(x => billIDs.Contains(x.BillID));
            }

            //单据创建日期
            if (filterModel._BillCreateDate_Begin.HasValue && filterModel._BillCreateDate_End.HasValue)
            {
                var billIDs = _DB.Queryable<Bill>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.CreateDate >= filterModel._BillCreateDate_Begin.Value && x.CreateDate < filterModel._BillCreateDate_End.Value.AddDays(1))
                 .Select(x => x.BillID).Distinct().ToList();

                where = where.And(x => billIDs.Contains(x.BillID));
            }

            //单据申请日期
            if (filterModel._BillDate_Begin.HasValue && filterModel._BillDate_End.HasValue)
            {
                var billIDs = _DB.Queryable<Bill>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && x.BillDate >= filterModel._BillDate_Begin.Value && x.BillDate < filterModel._BillDate_End.Value.AddDays(1))
                 .Select(x => x.BillID).Distinct().ToList();

                where = where.And(x => billIDs.Contains(x.BillID));
            }

            //单据号
            if (!filterModel._BillCode.IsEmpty())
            {
                var billIDs = _DB.Queryable<Bill>().Where(x => x.GroupInfoID == _currentUser.CurrentUserInfo.LoginTypeID && filterModel._BillCode.Contains(x.Code))
                 .Select(x => x.BillID).Distinct().ToList();

                where = where.And(x => billIDs.Contains(x.BillID));
            }


            return where;
        }
        #endregion
    }
}
